package com.example.controller;

import com.example.bean.Orders;
import com.example.bean.Result;
import com.example.bean.ResultVo;
import com.example.service.impl.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @ClassName OrdersController
 * @Author 冯泽嘉
 * @Date 2021/9/29 20:40
 */

@RestController
@CrossOrigin
@RequestMapping("/api")
public class OrdersController {

    @Autowired
    OrderServiceImpl orderService;

    /**
     * 第一版
     */
    @RequestMapping(value = "/getOrdersAll", method = RequestMethod.GET)
    @CrossOrigin
    public ResultVo getOrdersAll() {
        return orderService.getOrdersAll();
    }

    @RequestMapping(value = "/getOrdersAllUser", method = RequestMethod.POST)
    @CrossOrigin
    public ResultVo getOrdersAllUser(@RequestParam("uid") int uid) {
        return orderService.getOrdersAllUser(uid);
    }

    @RequestMapping(value = "/getOrdersAllMerchant", method = RequestMethod.POST)
    @CrossOrigin
    public ResultVo getOrdersAllMerchant(@RequestParam("mid") int mid) {
        return orderService.getOrdersAllMerchant(mid);
    }

    @RequestMapping("/findOrderByOrderNumberUser")
    @CrossOrigin
    //根据订单号精准查询
    public ResultVo findOrderByOrderNumberUser(@RequestParam("orderNumber") String orderNumber,
                                               @RequestParam("uid") int uid) {
        if (orderNumber == null) {
            return orderService.getOrdersAllUser(uid);
        } else {
            return orderService.findOrderByOrderNumberUser(orderNumber, uid);
        }
    }

    @RequestMapping("/findOrderByOrderNumberMerchant")
    @CrossOrigin
    //根据订单号精准查询
    public ResultVo findOrderByOrderNumberMerchant(@RequestParam("orderNumber") String orderNumber,
                                                   @RequestParam("mid") int mid) {
        if (orderNumber == null) {
            return orderService.getOrdersAllMerchant(mid);
        } else {
            return orderService.findOrderByOrderNumberMerchant(orderNumber, mid);
        }
    }

    @PostMapping("/findOrderByMNameUser")
    @CrossOrigin
    //根据订单商品名模糊查询
    public ResultVo findOrderByMNameUser(@RequestParam("mName") String mName, @RequestParam("uid") int uid) {
        return orderService.findOrderByMNameUser(mName, uid);
    }

    @PostMapping("/findOrderByMNameMerchant")
    @CrossOrigin
    //根据订单商品名模糊查询
    public ResultVo findOrderByMNameMerchant(@RequestParam("mName") String mName, @RequestParam("mid") int mid) {
        return orderService.findOrderByMNameMerchant(mName, mid);
    }


    @RequestMapping("/deleteOrderByOrderNumber")
    @CrossOrigin
    //根据订单号删除订单
    public ResultVo deleteOrderByOrderNumber(@RequestParam("orderNumber") String orderNumber) {
        return orderService.deleteOrderByOrderNumber(orderNumber);
    }

    /**
     * 商品购买
     *
     * @param uid
     * @param gid
     * @param mid
     */
//    @PostMapping(value = "/createNewOrders")
//    @CrossOrigin
//    public Result createNewOrders(@RequestParam("uid") int uid,
//                                  @RequestParam("gid") int gid,
//                                  @RequestParam("mid") int mid,
//                                  @RequestParam("gname") String mName
//    ) {
//        Orders order = new Orders();
//        order.setUId(uid);
//        order.setGId(gid);
//        order.setMId(mid);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String time = sdf.format(new Date());
//        order.setTime(time);
//        String orderNumber = String.valueOf(UUID.randomUUID());
//        order.setOrderNumber(orderNumber.substring(9));
//        order.setMName(mName);
//        boolean count = orderService.save(order);
//        if (count) {
//            return new Result(200, "购买成功", "");
//        } else {
//            return new Result(400, "购买失败", "");
//        }
//    }


    /**
     * 第二版（商家）
     */
    @RequestMapping(value = "/findOrdersAllMerchant2", method = RequestMethod.POST)
    @CrossOrigin
    //根据商家id查询订单
    public ResultVo findOrdersAllMerchant2(@RequestParam("mid") int mid) {
        return orderService.findOrdersAllMerchant2(mid);
    }

    @RequestMapping(value = "/findOrderByOrderNumberMerchant2", method = RequestMethod.POST)
    @CrossOrigin
    //商家根据订单号精准查询订单列表
    public ResultVo findOrderByOrderNumberMerchant2(@RequestParam("orderNumber") String orderNumber,
                                                    @RequestParam("mid") int mid) {
        return orderService.findOrderByOrderNumberMerchant2(orderNumber, mid);
    }

    @RequestMapping(value = "/findOrderByNameMerchant2", method = RequestMethod.POST)
    @CrossOrigin
    //商家根据商品名模糊查询订单列表
    public ResultVo findOrderByNameMerchant2(@RequestParam("gName") String gName,
                                             @RequestParam("mid") int mid) {
        return orderService.findOrderByNameMerchant2(gName, mid);
    }

    @RequestMapping(value = "/modifyOrderMerchant2", method = RequestMethod.POST)
    @CrossOrigin
    //商家修改用户收货地址
    public ResultVo modifyOrderMerchant2(@RequestParam("uid") int uid,
                                         @RequestParam("gid") int gid,
                                         @RequestParam("mid") int mid,
                                         @RequestParam("oName") String oName,
                                         @RequestParam("oPhone") String oPhone,
                                         @RequestParam("oAddress") String oAddress,
                                         @RequestParam("cNumber") String cNumber) {
        return orderService.modifyOrderMerchant2(uid, gid, mid, oName, oPhone, oAddress, cNumber);
    }

    @RequestMapping(value = "/changeOrderState2", method = RequestMethod.POST)
    @CrossOrigin
    //商家改变用户订单状态
    public ResultVo changeOrderState2(@RequestParam("oState") String oState,
                                      @RequestParam("uid") int uid,
                                      @RequestParam("gid") int gid,
                                      @RequestParam("mid") int mid,
                                      @RequestParam("cNumber") String cNumber) {
        return orderService.changeOrderState2(oState, uid, gid, mid, cNumber);
    }

    /**
     * 第二版（用户）
     */
    @RequestMapping(value = "/findOrdersAllUser2", method = RequestMethod.POST)
    @CrossOrigin
    //根据用户id查询订单
    public ResultVo findOrdersAllUser2(@RequestParam("uid") int uid) {
        return orderService.findOrdersAllUser2(uid);
    }

    @RequestMapping(value = "/findOrderByOrderNumberUser2", method = RequestMethod.POST)
    @CrossOrigin
    //用户根据订单号精准查询订单列表
    public ResultVo findOrderByOrderNumberUser2(@RequestParam("orderNumber") String orderNumber,
                                                @RequestParam("uid") int uid) {
        return orderService.findOrderByOrderNumberUser2(orderNumber, uid);
    }

    @RequestMapping(value = "/findOrderByNameUser2", method = RequestMethod.POST)
    @CrossOrigin
    //用户根据商品名模糊查询订单列表
    public ResultVo findOrderByNameUser2(@RequestParam("gName") String gName,
                                         @RequestParam("uid") int uid) {
        return orderService.findOrderByNameUser2(gName, uid);
    }

    @RequestMapping(value = "/findOrderByStateUser2", method = RequestMethod.POST)
    @CrossOrigin
    //用户根据商品名模糊查询订单列表
    public ResultVo findOrderByStateUser2(@RequestParam("oState") String oState,
                                          @RequestParam("uid") int uid) {
        return orderService.findOrderByStateUser2(oState, uid);
    }

    @PostMapping(value = "/createNewOrders2")
    @CrossOrigin
    //购买生成订单
    public Result createNewOrders2(@RequestParam("uid") int uid,
                                   @RequestParam("gid") int gid,
                                   @RequestParam("mid") int mid,
                                   @RequestParam("oName") String oName,
                                   @RequestParam("oPhone") String oPhone,
                                   @RequestParam("oAddress") String oAddress
    ) {
        Orders order = new Orders();
        order.setUId(uid);
        order.setGId(gid);
        order.setMId(mid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = sdf.format(new Date());
        order.setTime(time);
        String orderNumber = String.valueOf(UUID.randomUUID());
        order.setOrderNumber(orderNumber.substring(9));
        order.setOState("未发货");
        order.setOName(oName);
        order.setOPhone(oPhone);
        order.setOAddress(oAddress);
        boolean count = orderService.save(order);
        //调用减少库存的方法，判断库存是否减少成功
        boolean update = orderService.reInventory(gid);
        if (count && update) {
            return new Result(200, "购买成功", "");
        } else {
            return new Result(400, "购买失败", "");
        }
    }

    /**
     * 查找最新生成的订单号
     *
     * @return
     */
    @PostMapping(value = "findNewstOrder")
    @CrossOrigin
    public Result findNewstOrder(@RequestParam("uid") int uid) {
        String newstOrderNumber = orderService.findNewstOrder(uid);
        return new Result(200, "", newstOrderNumber);
    }

    /**
     * 根据订单状态、用户id、订单号修改订单状态
     *
     * @param uid         用户id
     * @param orderNumber 订单号
     * @param orderstate  订单状态
     * @return
     */
    @PostMapping(value = "/ChangeStateUser")
    @CrossOrigin
    public ResultVo ChangeStateUser(@RequestParam("uid") int uid, @RequestParam("orderNumber") String orderNumber, @RequestParam("orderstate") String orderstate) {
        return orderService.ChangeStateUser(orderstate, uid, orderNumber);
    }

}
